﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace BLL
{
   public static  class 委外取货单BLL
    {
        public static void 保存单据(DanjuTable danju,List<danjumingxitable > danjumingxitables )
        {
            danju.zhuangtai = "已审核";
            Connect.dbcontext.Insertable(danju).ExecuteCommand ();
            foreach (var d in danjumingxitables.Where(x=>!string.IsNullOrEmpty( x.pingming)).ToList ())
            {
                d.danhao = danju.dh;              
            }
            Connect.dbcontext.Insertable(danjumingxitables.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList()).ExecuteCommand();        
            单据审核(danju.dh);

        }
        public static void 修改单据(DanjuTable danju, List<danjumingxitable> danjumingxitables)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danju.dh);
                }
            }
            else
            {
                单据反审核(danju.dh);
            }
            DanjuTableService.UpdateDanjuTable (danju,x=>x.dh==danju.dh );
            danjumingxitableService.Deletedanjumingxitable(x => x.danhao == danju.dh);
            Connect.dbcontext.Insertable(danjumingxitables.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList()).ExecuteCommand();
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju.dh);
                }
            }
        }
        public static List<string> 获取加工类型()
        {
           return ShengchandanGongyiService.GetShengchandanGongyilst().Select(x=>x.gongyimingcheng ).ToList ();
        }
        public static void 单据审核(string danhao)
        {
                var danju = Connect.dbcontext.Queryable<DanjuTable>().First (x => x.dh == danhao);
                var danjumingxitables = Connect.dbcontext.Queryable<danjumingxitable>().Where(x => x.danhao == danhao).ToList ();                
                来往明细BLL.增加来往记录(danju);
                财务BLL.增加应付款(danju);
                财务BLL.增加应收发票(danju);
                库存BLL.增加库存(danjumingxitables, danju);
                danju.ckmc = danju.ksmc;
                库存BLL.减少库存(danjumingxitables, danju);
        }
        public static void 单据反审核(string danhao)
        {            
                var danju = DanjuTableService.GetOneDanjuTable(x => x.dh == danhao);
                var danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danhao);            
                DanjuTableService.UpdateDanjuTable("zhuangtai='未审核'", x => x.dh == danhao);
                来往明细BLL.删除来往记录(danju);
                财务BLL.减少应付款(danju);
                财务BLL.减少应收发票(danju);
                订单BLL.减少费用(danjumingxitables, danju);
                库存BLL.减少库存(danjumingxitables, danju);
                单据BLL.未审核(danhao);
                danju.ckmc = danju.ksmc;
                库存BLL.增加库存(danjumingxitables, danju);
        }
        public static void 删除单据(string danhao)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danhao);
                }
            }
            else
            {
                单据反审核(danhao);
            }
            DanjuTableService.DeleteDanjuTable(x => x.dh == danhao);
            danjumingxitableService.Deletedanjumingxitable(x => x.danhao == danhao);
        }
    }
}
